package com.google.code.head2head.gwtmodules.client.imagepanels;

import java.util.Iterator;
import java.util.List;

import com.google.code.head2head.gwtmodules.client.rating.RateItWidget;
import com.google.code.head2head.gwtmodules.client.services.FacadeServicesAsync;
import com.google.code.head2head.gwtmodules.client.util.ServiceLocator;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.ChangeListener;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.KeyboardListener;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.gwtext.client.widgets.MessageBox;

public class AllEntriesPanel extends FlowPanel {

	private List entries;

	private VerticalPanel entriesPanel = new VerticalPanel();

	final private TextBox tagsBox = new TextBox();

	public AllEntriesPanel() {

		initWidget();

	}

	private void initWidget() {
		initTagsBox();
		add(new HTML("Enter keywords to find pictures"));
		add(tagsBox);
		initEntries(null);
		add(entriesPanel);
	}

	private void initTagsBox() {
		tagsBox.addKeyboardListener(new KeyboardListener() {

			public void onKeyDown(Widget arg0, char arg1, int arg2) {
				// TODO Auto-generated method stub

			}

			public void onKeyPress(Widget arg0, char arg1, int arg2) {
				// TODO Auto-generated method stub

			}

			public void onKeyUp(Widget arg0, char arg1, int arg2) {
				if (arg1 == KeyboardListener.KEY_ENTER) {
					String[] tags = tagsBox.getText().split("\\s");
					initEntries(tags);
				}

			}

		});

	}

	private void initEntries(String[] tags) {
		FacadeServicesAsync svc = ServiceLocator.getFacadeServicesAsync();
		AsyncCallback callback = new AsyncCallback() {

			public void onSuccess(Object result) {
				entriesPanel.clear();
				entries = (List) result;
				fillEntriesPanel();
			}

			public void onFailure(Throwable ex) {
				MessageBox.alert("Message", "Problem loading entries");
			}
		};
		// execute the service
		svc.getAllEntries(tags, callback);
	}

	private void fillEntriesPanel() {
		entriesPanel.clear();
		for (Iterator iterator = entries.iterator(); iterator.hasNext();) {
			com.google.code.head2head.gwtmodules.client.dto.Entry entry = (com.google.code.head2head.gwtmodules.client.dto.Entry) iterator
					.next();
			float meanRating = entry.getMeanRating();
			entriesPanel.add(new DisclosureTwoImagePanel(entry
					.getFistImageUrl(), entry.getSecondImageUrl()));
			entriesPanel.add(createRateWidget(entry.getId(), meanRating));

			if (meanRating != -1) {
				entriesPanel.add(new HTML("Mean rating: " + meanRating));
			} else {
				entriesPanel.add(new HTML("Not rated yet"));
			}
		}
	}

	private RateItWidget createRateWidget(final Long entryId, float meanRating) {
		Image[] ratingImages = new Image[] { new Image("images/starfull.gif"),
				new Image("images/starempty.gif") };
		final RateItWidget rateIt = new RateItWidget(meanRating, 5, new Image(
				"images/starempty.gif"), new Image("images/starfull.gif"),
				new Image("images/donut.gif"),
				new Image("images/donutred.gif"), ratingImages);

		rateIt.addChangeListener(new ChangeListener() {
			public void onChange(Widget sender) {

				FacadeServicesAsync svc = ServiceLocator.getFacadeServicesAsync();
				AsyncCallback callback = new AsyncCallback() {

					public void onFailure(Throwable arg0) {
						MessageBox.alert("Message",
								"Problem during recording of user rating");
					}

					public void onSuccess(Object arg0) {
						fillEntriesPanel();
						MessageBox.alert("Message", (String) arg0);
					}

				};
				svc.rateEntry(entryId, rateIt.getUserRating(), callback);
			}
		});
		return rateIt;
	}

}
